#include "Windows_MainFrame.hpp"

#include "InterAct/Window/Window.hpp"

Windows_MainFrame::Windows_MainFrame() {
    Gdiplus::GdiplusStartup(&mGdi_Token, &mGdi_Startup_Input, nullptr);
}

Windows_MainFrame::~Windows_MainFrame() {
    Gdiplus::GdiplusShutdown(mGdi_Token);
}

LRESULT CALLBACK Windows_MainFrame::Window_Procedure(HWND pHwnd, UINT pMsg, WPARAM pWParam, LPARAM pLParam) {
    switch (pMsg) {
        case WM_DESTROY:
            Window_Procedure_Destroy(pHwnd, pMsg, pWParam, pLParam);
        case WM_COMMAND:
            Window_Procedure_Command(pHwnd, pMsg, pWParam, pLParam);
        default:
            return DefWindowProc(pHwnd, pMsg, pWParam, pLParam);
        }
}

LRESULT CALLBACK Windows_MainFrame::Window_Procedure_Destroy(HWND pHwnd, UINT pMsg, WPARAM pWParam, LPARAM pLParam) {
    free_WindowBase_Set();
    PostQuitMessage(0);
    return 0;
}

LRESULT CALLBACK Windows_MainFrame::Window_Procedure_Command(HWND pHwnd, UINT pMsg, WPARAM pWParam, LPARAM pLParam) {
    switch (LOWORD(pWParam)) {
        case BUTTON_ID::RUN:
            switch (HIWORD(pWParam)) {
                case BN_CLICKED: {
                    acct
                        .set_Process(GetCurrentProcess())
                        .enable_Access_desired_Query()
                        .query_ProcessToken()
                        .set_TokenInformationType_toUserAccount()
                        .query_TokenInformation()
                    ;

                    
                }
                break;
                default:
                return 0;
            }
        break;
        case BUTTON_ID::RUN2:
            switch (HIWORD(pWParam)) {
                case BN_CLICKED: {
                    d.find_next();
                    d.find_print();
                }
                break;
                default:
                return 0;
            }
        break;
        case BUTTON_ID::CLOSE:
            switch (HIWORD(pWParam)) {
                case BN_CLICKED: {
                    DestroyWindow(mWindowBase_Set[0]->get_WindowHandle());
                }
                break;
                default:
                return 0;
            }
        break;
        default:
        return 0;
    }
    return 0;
}

int Windows_MainFrame::run(HINSTANCE pInstance, HINSTANCE pPreviousInstance, PSTR pCmdLine, INT pCmdShow) {

    set_WndClass_Name("MainWindowClass");
    set_WndClass_Instance(pInstance);
    set_WndClass_Background((HBRUSH)COLOR_WINDOW);
    register_WndClass();

    Agx::Window* mainWindow = new Agx::Window(pInstance);
    mainWindow->set_WindowTitle("MainWindow");
    mainWindow->set_WndClass_Name(get_WndClass_Name());
    add_toWindowBase_Set(mainWindow);
    mainWindow->show();

    Agx::Window* child = new Agx::Window(pInstance);
    child->set_WindowTitle("ChildWindow");
    child->set_WndClass_Name(get_WndClass_Name());
    child->set_Heigth(300);
    child->set_Width(300);
    child->set_X_Position(100);
    child->set_Y_Position(100);
    child->set_Parent(mainWindow->get_WindowHandle());
    child->enable_ThinLine_Border();
    add_toWindowBase_Set(child);
    child->show();
    
    Agx::Button* b = new Agx::Button(pInstance, mainWindow->get_WindowHandle());
    b->set_X_Position(20);
    b->set_Y_Position(20);
    b->set_ButtonText("Run");
    b->set_Button_ID(BUTTON_ID::RUN);
    add_toWindowBase_Set(b);
    b->show();

    Agx::Button* b2 = new Agx::Button(pInstance, mainWindow->get_WindowHandle());
    b2->set_X_Position(140);
    b2->set_Y_Position(20);
    b2->set_ButtonText("Run");
    b2->set_Button_ID(BUTTON_ID::RUN2);
    add_toWindowBase_Set(b2);
    b2->show();

    Agx::Button* close = new Agx::Button(pInstance, mainWindow->get_WindowHandle());
    close->set_X_Position(300);
    close->set_Y_Position(20);
    close->set_ButtonText("Close");
    close->set_Button_ID(BUTTON_ID::CLOSE);
    add_toWindowBase_Set(close);
    close->show();

    MSG msg = {};
    while(GetMessageA(&msg, nullptr, 0, 0)) {
        TranslateMessage(&msg);
        DispatchMessage(&msg);
    }

    return 0;
}

